//
//  ZLTogetherLearnHomeViewController.m
//  ZLExamination
//
//  Created by leilei on 2017/8/15.
//  Copyright © 2017年 Zheng. All rights reserved.
//

#import "ZLTogetherLearnHomeViewController.h"
#import "ZLTogetherLearnHomeTableViewCell.h"
#import "ZLTogetherLearnModel.h"
#import "ZLCourseLerningDetailViewController.h"


static NSString *const ZLTogetherLearnHomeTableViewCellID = @"ZLTogetherLearnHomeTableViewCell";

@interface ZLTogetherLearnHomeViewController ()

//数据
@property (nonatomic, strong) NSMutableArray<ZLTogetherLearnModel *> *dataArr;

@end

@implementation ZLTogetherLearnHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.myTableView.backgroundColor = [UIColor whiteColor];
    
    [self.myTableView registerClass:[ZLTogetherLearnHomeTableViewCell class] forCellReuseIdentifier:ZLTogetherLearnHomeTableViewCellID];

    [self loadData];
    
}

#pragma mark - 加载数据
- (void)loadData {
    
    _dataArr = [NSMutableArray new];
    
    NSDictionary *param;
    
    if (_isLearning) {//正在学
        param = @{@"status":@2};
    } else {
        param = @{@"type":_type};
    }
    
    [[YCRequestDataService sharedManager] Post:@"curriculum/curriculum/lists" parameters:param success:^(id response) {
        if (response[@"error_code"]) {
            NSLog(@"%@",response[@"error_msg"]);
        } else {
            NSArray<NSDictionary *> *lists = response[@"response_data"][@"lists"];
            [lists enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                ZLTogetherLearnModel *model = [[ZLTogetherLearnModel alloc] initWithDataDic:obj];
                [_dataArr addObject:model];
            }];
            
            [self.myTableView reloadData];
        }
    } failure:^{
        NSLog(@"网络或服务器出错");
    }];
}


#pragma mark - tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZLTogetherLearnHomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ZLTogetherLearnHomeTableViewCellID forIndexPath:indexPath];
    
    if (_dataArr.count != 0) {
        cell.model = _dataArr[indexPath.row];
    }
    
    return cell;
}

#pragma mark - tableview delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    return 70;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    kCX_LOG(@"课程学习%ld",indexPath.row);
    
    ZLCourseLerningDetailViewController *cldVC = [[ZLCourseLerningDetailViewController alloc] init];
    ZLTogetherLearnModel *model = _dataArr[indexPath.row];
    cldVC.curriculum_id = model.u_id;
    cldVC.navigationItem.title = model.title;
    [self.navigationController pushViewController:cldVC animated:YES];
    
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 3) {
        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:UIEdgeInsetsZero];
        }
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
            [cell setSeparatorInset:UIEdgeInsetsZero];
        }
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
